package haoyou.csnf.manager.module.pm.service.professionaltitle;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import haoyou.csnf.manager.module.pm.controller.admin.professionaltitle.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.professionaltitle.ProfessionalTitleDO;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;

import haoyou.csnf.manager.module.pm.dal.mysql.professionaltitle.ProfessionalTitleMapper;

import static haoyou.csnf.manager.framework.common.exception.util.ServiceExceptionUtil.exception;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.convertList;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.diffList;
import static haoyou.csnf.manager.module.pm.enums.ErrorCodeConstants.*;

/**
 * 系统职称 Service 实现类
 *
 * @author 好有经验源码
 */
@Service
@Validated
public class ProfessionalTitleServiceImpl implements ProfessionalTitleService {

    @Resource
    private ProfessionalTitleMapper professionalTitleMapper;

    @Override
    public Long createProfessionalTitle(ProfessionalTitleSaveReqVO createReqVO) {
        // 插入
        ProfessionalTitleDO professionalTitle = BeanUtils.toBean(createReqVO, ProfessionalTitleDO.class);
        professionalTitleMapper.insert(professionalTitle);
        // 返回
        return professionalTitle.getId();
    }

    @Override
    public void updateProfessionalTitle(ProfessionalTitleSaveReqVO updateReqVO) {
        // 校验存在
        validateProfessionalTitleExists(updateReqVO.getId());
        // 更新
        ProfessionalTitleDO updateObj = BeanUtils.toBean(updateReqVO, ProfessionalTitleDO.class);
        professionalTitleMapper.updateById(updateObj);
    }

    @Override
    public void deleteProfessionalTitle(Long id) {
        // 校验存在
        validateProfessionalTitleExists(id);
        // 删除
        professionalTitleMapper.deleteById(id);
    }

    @Override
        public void deleteProfessionalTitleListByIds(List<Long> ids) {
        // 校验存在
        validateProfessionalTitleExists(ids);
        // 删除
        professionalTitleMapper.deleteByIds(ids);
        }

    private void validateProfessionalTitleExists(List<Long> ids) {
        List<ProfessionalTitleDO> list = professionalTitleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PROFESSIONAL_TITLE_NOT_EXISTS);
        }
    }

    private void validateProfessionalTitleExists(Long id) {
        if (professionalTitleMapper.selectById(id) == null) {
            throw exception(PROFESSIONAL_TITLE_NOT_EXISTS);
        }
    }

    @Override
    public ProfessionalTitleDO getProfessionalTitle(Long id) {
        return professionalTitleMapper.selectById(id);
    }

    @Override
    public PageResult<ProfessionalTitleDO> getProfessionalTitlePage(ProfessionalTitlePageReqVO pageReqVO) {
        return professionalTitleMapper.selectPage(pageReqVO);
    }

}